<template>
  <div class="addGoods">
    <el-card class="box-card">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/goods' }"
          >商品列表</el-breadcrumb-item
        >
        <el-breadcrumb-item>添加商品</el-breadcrumb-item>
      </el-breadcrumb>
      <el-steps
        :active="active"
        finish-status="success"
        simple
        style="margin-top: 20px"
      >
        <el-step title="步骤 1"></el-step>
        <el-step title="步骤 2"></el-step>
        <el-step title="步骤 3"></el-step>
        <el-step title="步骤 4"></el-step>
      </el-steps>
      <el-tabs
        v-model="activeName"
        @tab-click="handleClick"
        tab-position="left"
        class="mt-20"
      >
        <el-tab-pane label="基本信息" name="1">
          <el-form label-width="80px" v-model="goodsInfo" :rules="rules">
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="goodsInfo.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input v-model="goodsInfo.goods_price"></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input v-model="goodsInfo.goods_number"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input v-model="goodsInfo.goods_weight"></el-input>
            </el-form-item>
            <el-form-item label="商品介绍" prop="goods_introduce">
              <el-input v-model="goodsInfo.goods_introduce"></el-input>
            </el-form-item>
          </el-form>
          <div class="footer">
            <el-button type="primary" @click="(activeName = '2'), (active = 1)"
              >下一步</el-button
            >
          </div>
        </el-tab-pane>
        <el-tab-pane label="商品分类" name="2"
          ><div class="block">
            <span class="demonstration">请选择分类</span>
            <el-cascader
              v-model="value"
              :options="options"
              :props="props"
              @change="handleChange"
            ></el-cascader>
          </div>
          <div class="footer">
            <el-button @click="(activeName = '1'), (active = 0)"
              >上一步</el-button
            >
            <el-button type="primary" @click="(activeName = '3'), (active = 2)"
              >下一步</el-button
            >
          </div>
        </el-tab-pane>
        <el-tab-pane label="商品图片" name="3">
          <el-upload
            action="http://ludir.cn:8888/api/private/v1/goods"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :on-success="handleSuccess"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
          <div class="footer">
            <el-button @click="(activeName = '2'), (active = 1)"
              >上一步</el-button
            >
            <el-button type="primary" @click="(activeName = '4'), (active = 3)"
              >下一步</el-button
            >
          </div>
        </el-tab-pane>
        <el-tab-pane label="商品参数" name="4">
          <el-form label-width="80px" v-model="goodsAttr">
            <el-form-item label="参数" prop="attrs">
              <el-input
                placeholder="可以写多个,以逗号隔开"
                v-model="goodsAttr.attrs"
              ></el-input>
            </el-form-item>
          </el-form>
          <div class="footer">
            <el-button @click="(activeName = '3'), (active = 2)"
              >上一步</el-button
            >
            <el-button type="primary" @click="addGoods">提交</el-button>
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getCategoriesList, addGoods } from "@/api/index";
export default {
  name: "",
  data() {
    return {
      active: 0,
      activeName: "1",
      value: "",
      props: {
        value: "cat_id",
        label: "cat_name"
      },
      options: [],
      dialogImageUrl: "",
      dialogVisible: false,
      goodsInfo: {
        goods_name: "",
        goods_price: "",
        goods_number: "",
        goods_weight: "",
        goods_introduce: "",
        goods_cat: ""
      },
      goodsAttr: {
        attr: ""
      },
      params: {
        goods_cat: "",
        attrs: [],
        pics: ""
      },
      rules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" }
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" }
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" }
        ],
        goods_cat: [
          { required: true, message: "请选择商品分类", trigger: "blur" }
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" }
        ]
      }
    };
  },
  watch: {},
  props: {},
  components: {},
  computed: {},
  mounted() {},
  methods: {
    handleClick(tab, event) {
      switch (tab.name) {
        case "1":
          this.active = 0;
          break;
        case "2":
          this.active = 1;
          break;
        case "3":
          this.active = 2;
          break;
        case "4":
          this.active = 3;
          break;
        default:
          this.active = 0;
          break;
      }
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    handleChange(value) {
      this.value = value;
      this.goodsInfo.goods_cat = value.join();
    },
    // 获取商品分类列表
    async initList() {
      await getCategoriesList(this.queryInfo).then(({ data, meta }) => {
        if (meta.status == 200) {
          this.options = data;
        } else {
          this.$message.error(meta.msg);
        }
      });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleSuccess(response, file) {
      this.params.pics = file.url;
    },
    // 添加商品
    async addGoods() {
      this.params = {
        ...this.params,
        ...this.goodsInfo,
        ...this.goodsAttr
      };
      await addGoods(this.params).then(({ meta }) => {
        if (meta.status == 200) {
          this.$message.success(meta.msg);
        } else {
          this.$message.error(meta.msg);
        }
      });
    }
  },
  filters: {},
  created() {
    this.initList();
  }
};
</script>

<style scoped lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-tab-pane {
  text-align: left;
  padding: 20px;
}
.el-input {
  width: 30%;
}
.footer {
  width: 35%;
  padding: 20px;
}
</style>
